﻿var showTable;
function removeFile(obj, txtID) {
    if (showTable == null || showTable == undefined) {
        showTable = $(obj).parents("table").find("table").first();
    }
    $(obj).parent().animate({ height: 0 }, 300, function() { $(obj).parent().parent().parent().remove(); });
    $(obj).parent().parent().prev().find("div").animate({ height: 0 }, 300);
    SetValue(txtID);
}
function SetValue(txtID) {
    var value = "<files>";
    showTable.find("tr").each(function() {
        var file = $(this).find("a").attr("href");
        var index = file.lastIndexOf("/");
        var name = file.substr(index + 1, file.length - index - 1);
        value += "<file name=\"" + name + "\" path=\"" + file + "\"></file>";
    });
    value += "</files>";
    value = showTable.find("tr").length == 0 ? "" : value;
    $("#" + txtID).val(value);
}

function xfinderuploder(txtID, dom, filetypes, folder, fileLimit) {

    showTable = $(dom).parents("table").find("table").first();
    if (showTable.find("tr").length >= fileLimit&&fileLimit!=1) {
        alert("该字段最多允许上传数量为:" + fileLimit);
        return;
    }
    var url = "/Manage/XFinder.aspx?cp=" + folder + "&filetypes=" + filetypes + "&callback=callback" + $(dom).attr("id") + "&fileLimit=" + fileLimit;
    var guid = AddWindow("选择文件", url,
    {
        refresh: false,
        width: 640,
        height: 470,
        ResizeStopFunc: function(iframeobj, w, h) {
            var body = iframeobj.find("body");
            //body.width(w);
            //alert(w + "   " + body.find("#divContainer").width());
            body.find("#divContainer").height(h - 145);//.width(w - 15).parent().width(w -15);
            //$("#tablefirst").width(w);
        }
    });
    window["callback" + $(dom).attr("id")] = function(files) {
        CloseWindow(guid, false);
        if (files != "") {
            if (fileLimit == 1) {
                $(dom).parent().prev().children().val(files);
            }
            else {
                var fileArray = files.split(',');
                for (var i = 0; i < fileArray.length; i++) {
                    var file = fileArray[i];
                    var regImg = /^.*?\.(jpg|jpeg|png|bmp|gif)$/i;
                    var src = file;
                    if (!regImg.test(src)) {
                        src = "/Manage/Resources/default/images/file.gif";
                    }
                    showTable.append("<tr><td><div><a target='_blank' href='" + file + "'><img width='40' height='40' style='vertical-align: middle;' src='" +
                                src + "' />" + file + "</a></div></td><td><div><input type='button' onclick=\"removeFile(this,'" + txtID + "')\" value='删除' /></div></td></tr>");
                }
                if (showTable.find("tr").length > fileLimit&&fileLimit!=-1) {
                    showTable.find("tr").slice(fileLimit).remove();
                }
                SetValue(txtID);
            }
        }
    }
}
//function viewpic() {
//    $("#DisplayImg").lightBox({ txtOf: "/" });
//    
//    return false;
//}
//setTimeout(function() {
//    $("head").append("<script type='text/javascript' src='/Script/plugin/lightbox/js/jquery.lightbox-0.5.js' />");
//    $("head").append("<link rel='stylesheet' type='text/css' href='/Script/plugin/lightbox/css/jquery.lightbox.css' />");    
//    $("body").append("<a id='DisplayImg' href='' style='display:none;'></a>");
//    viewpic();
//}, 2000);
//$(".divShowImg a").click(function() { DisplayImg($(this)); });
function DisplayImg(obj) {
    var imgSrc = $(obj).parent().prevAll().last().children().val();
    var reg = /^.*\.([jJ][pP][Gg]|[gG][iI][Ff]|[Pp][Nn][Gg])$/;
    if (new RegExp(reg).test(imgSrc)) {
        window.open(imgSrc);
    } else {
        alert("此类型不能查看");
    }

}